//
//  ExhibitorLoginViewController.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/3/3.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "ExhibitorLoginViewController.h"
#import "XXTool.h"
#import "VisitorRigisterModel.h"
#import "PresentProjectMainCotroller.h"
#import "PersonInfoViewController.h"

@interface ExhibitorLoginViewController ()

@property (nonatomic) UIScrollView* background;

@property (nonatomic) UIView *content;

@property (nonatomic) UILabel *topTipLabel;

@property (nonatomic) ProjectOnlyTextField *inputAccount;

@property (nonatomic) ProjectOnlyTextField *inputPassword;

@property (nonatomic) DKButton* login;

@property(nonatomic,strong) UIImageView* line1;

@property(nonatomic,strong) UIImageView* line2;

@end

@implementation ExhibitorLoginViewController
#pragma mark UI
-(UIScrollView*)background
{
    if (_background==nil)
    {
        _background = [[UIScrollView alloc] init];
        _background.backgroundColor = [UIColor clearColor];
        [self.view addSubview:_background];
    }
    return _background;
}

-(UIView*)content
{
    if (_content==nil)
    {
        _content = [[UIView alloc] init];
        _content.backgroundColor = [UIColor clearColor];
        [_content addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickBackground)]];
        _content.userInteractionEnabled = YES;
        [_background addSubview:_content];
    }
    return _content;
}


-(UILabel*)topTipLabel
{
    if (_topTipLabel==nil)
    {
        _topTipLabel = [[UILabel alloc] init];
        _topTipLabel.font = [XXTool systemFontOfSize:15];
        _topTipLabel.text = NSLocalizedString(@"*和展商手册上的展商登录账号密码一致。", nil);
        _topTipLabel.textColor = [XXTool colorProjectWithAlpha:0.7];
        _topTipLabel.numberOfLines = 0;
        [_background addSubview:_topTipLabel];
    }
    return _topTipLabel;
}

-(ProjectOnlyTextField*)inputAccount
{
    if (_inputAccount==nil)
    {
        UIView *inputLeftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 5)];
        
        _inputAccount = [[ProjectOnlyTextField alloc] init];
        _inputAccount.textColor = [XXTool colorWithProject];
        _inputAccount.leftView = inputLeftView;
        _inputAccount.leftViewMode = UITextFieldViewModeAlways;
//        _inputAccount.layer.borderWidth = 1;
//        _inputAccount.layer.borderColor = [XXTool colorWithProject].CGColor;
//        _inputAccount.layer.cornerRadius = 5;
        _inputAccount.font = [XXTool systemFontOfSize:17];
        _inputAccount.placeholder = NSLocalizedString(@"请输入账号", nil);
        [_inputAccount setValue:[XXTool colorProjectWithAlpha:0.3] forKeyPath:@"_placeholderLabel.textColor"];
        [_background addSubview:_inputAccount];
    }
    return _inputAccount;
}

-(UIImageView*) line1{
    if(_line1 == nil){
        _line1=[[UIImageView alloc]initWithImage:[[UIImage imageNamed:@"长线"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
        _line1.tintColor = [XXTool colorWithProject];
        [_background addSubview:_line1];
    }
    return _line1;
}

-(ProjectOnlyTextField*)inputPassword
{
    if (_inputPassword==nil)
    {
        UIView *inputLeftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 5)];
        
        _inputPassword = [[ProjectOnlyTextField alloc] init];
        _inputPassword.textColor = [XXTool colorWithProject];
        _inputPassword.font = [XXTool systemFontOfSize:17];
//        _inputPassword.layer.borderWidth = 1;
//        _inputPassword.layer.borderColor = [XXTool colorWithProject].CGColor;
//        _inputPassword.layer.cornerRadius = 5;
        _inputPassword.leftView = inputLeftView;
        _inputPassword.secureTextEntry = YES;
        _inputPassword.leftViewMode = UITextFieldViewModeAlways;
        _inputPassword.placeholder = NSLocalizedString(@"请输入登录密码", nil);
        [_inputPassword setValue:[XXTool colorProjectWithAlpha:0.3] forKeyPath:@"_placeholderLabel.textColor"];
        [_background addSubview:_inputPassword];
    }
    return _inputPassword;
}

-(UIImageView*) line2{
    if(_line2 == nil){
        _line2=[[UIImageView alloc]initWithImage:[[UIImage imageNamed:@"长线"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
        _line2.tintColor = [XXTool colorWithProject];
        [_background addSubview:_line2];
    }
    return _line2;
}

-(DKButton*)login
{
    if (_login==nil)
    {
        _login = [[DKButton alloc] init];
        _login.textColor = [UIColor whiteColor];
        _login.font = [XXTool systemFontOfSize:17];
        _login.text = NSLocalizedString(@"登录", nil);
        [_login setColor:[XXTool colorWithProject] ForState:DKButtonStateNormal];
        [_login addTarget:self Select:@selector(clickExhibitorLogin) ForEvent:DKButtonEventPressedEnd];
        _login.layer.cornerRadius = 25;
        [_background addSubview:_login];
    }
    return _login;
}


-(void)initParameter
{
    
}

-(void)initUI
{
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    [self.navigationController.navigationBar setHidden:NO];
    [self.navigationItem setTitle:NSLocalizedString(@"展商登录", nil)];
    //[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(clickleftBarCancel)]];
    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"跳过", nil) style:UIBarButtonItemStylePlain target:self action:@selector(clickSkipButton)]];
    [self background];
    [self content];
    [self topTipLabel];
    [self inputAccount];
    [self inputPassword];
    [self line1];
    [self line2];
    [self login];
   
}

-(void)refreshLayout:(CGRect)frame
{
    _background.frame = CGRectMake(0, 0, frame.size.width
                                   , frame.size.height);
    
    
    CGSize topTipLabelSize  = [_topTipLabel.text boundingRectWithSize:CGSizeMake(frame.size.width-30*2, MAXFLOAT)
                                                               options:NSStringDrawingUsesLineFragmentOrigin
                                                            attributes:@{NSFontAttributeName:_topTipLabel.font}
                                                               context:nil].size;
    _topTipLabel.frame = CGRectMake(30, 20
                                    , frame.size.width-30*2
                                    , topTipLabelSize.height);
    _inputAccount.frame = CGRectMake(30
                                     , _topTipLabel.frame.size.height+_topTipLabel.frame.origin.y+10
                                     , frame.size.width-30*2
                                     , 47);
    _line1.frame=CGRectMake(30, _inputAccount.frame.origin.y+_inputAccount.frame.size.height, _inputAccount.frame.size.width, 4);
    
    _inputPassword.frame = CGRectMake(_inputAccount.frame.origin.x
                                      , _inputAccount.frame.size.height+_inputAccount.frame.origin.y+14
                                      , _inputAccount.frame.size.width
                                      , _inputAccount.frame.size.height);
    _line2.frame=CGRectMake(_inputPassword.frame.origin.x, _inputPassword.frame.origin.y+_inputPassword.frame.size.height, _inputPassword.frame.size.width, 4);
    
    _login.frame = CGRectMake(_inputPassword.frame.origin.x
               , _inputPassword.frame.origin.y+_inputPassword.frame.size.height+30
               , _inputPassword.frame.size.width
               , _inputPassword.frame.size.height);
    
    _background.contentSize = CGSizeMake(frame.size.width
                                         , _login.frame.size.height+_login.frame.origin.y+20);
    _content.frame = CGRectMake(0, 0, _background.contentSize.width
                                , _background.contentSize.height>_background.frame.size.height?_background.contentSize.height:_background.frame.size.height);
    
}

-(void)clickBackground
{
    [self.inputAccount resignFirstResponder];
    [self.inputPassword resignFirstResponder];
}

/*
-(void)clickleftBarCancel
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
 */

/**
 *  点击跳过
 */
-(void)clickSkipButton
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"successLoginAndFirstIntro" object:nil userInfo:@{@"is":[NSNumber numberWithBool:self.isNeedGoIntro]}];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

/**
 *  点击展商登录
 */
-(void)clickExhibitorLogin
{
    if ([_inputAccount.text isEqualToString:@""])
    {
        [SVProgressHUD showInfoWithStatus:NSLocalizedString(@"请输入账号", nil)];
        return;
    }

    if ([_inputPassword.text isEqualToString:@""])
    {
        [SVProgressHUD showInfoWithStatus:NSLocalizedString(@"请输入登录密码", nil)];
        return;
    }
    
    VistorRigiterUseDataModel *temp = [[VistorRigiterUseDataModel alloc] init];
    temp.phoneNumberOrEmail = _inputAccount.text;
    temp.password = _inputPassword.text;
    [VisitorRigisterModel exhibitorLogin:temp
                              completion:^(BOOL isSuccess) {
                                  if (isSuccess)
                                  {
                                      [[NSNotificationCenter defaultCenter] postNotificationName:@"exhibitorSuccessInfo" object:nil userInfo:@{@"is":[NSNumber numberWithBool:self.isNeedGoIntro]}];
                                      [self dismissViewControllerAnimated:YES completion:^{
                                          
                                      }];
//                                      PersonInfoViewController *vc = [[PersonInfoViewController alloc] init];
//                                      vc.isNeedGoIntro = YES;
//                                      vc.useType = PersonInfoViewControllerDidExhibitorPerfect;
//                                      [self.navigationController pushViewController:vc animated:YES];
                                  }
                              }];
}

@end
